<?php


Class PMsystem extends Database{

	private $uid;
	
	public function __construct($uid){
	
		parent::__construct();
		
		$this->uid = (int)$uid;
		
	}
	public function sendPM($pm_too,$pm_title,$pm_message){
		$this->create_row('pm',array('',$pm_too,$this->uid,$pm_too,$pm_title,$pm_message,'0'));//send to them
		$this->create_row('pm',array('',$this->uid,$this->uid,$pm_too,$pm_title,$pm_message,'1'));//keep a copy for us
	}
	public function getPM($pm_id){
		// PM_id, PM_to, PM_from, PM_title PM_Message
		$pm = $this->get_row('pm','pm_id',$pm_id);

		if((int)$pm['pm_owner'] !== $this->uid) return false;
		
		$this->set_row('pm','pm_id',$pm_id, array('viewed'=>1));
		
		return $pm;
	}

	public function listUserPms(){
		return $this->get_rows('pm',array('pm_id','pm_from','pm_too','title'),0,0,array('pm_too'=>$this->uid,'pm_owner'=>$this->uid));
	}
	public function listUserSentPms(){
		return $this->get_rows('pm',array('pm_id','pm_from','pm_too','title'),0,0,array('pm_from'=>$this->uid,'pm_owner'=>$this->uid));
	}
	
	public function count_new_pms(){
		return $this->get_rowcount('pm','pm_id',array('pm_id'=>$this->uid,'viewed'=>1));
	}
	
	public function removePM($pm_id){
		//safty 
		$pm = $this->getPM($pm_id);		
		
		if((int)$pm['pm_owner'] !== $this->uid) return false; // not owner of pm, cannot do this
		
		return $this->remove_row('pm','pm_id',$pm_id); //limit one! always
	}
}